The semantics of Thread
and Runnable
are different, and while it is technically correct to use Thread
where
a Runnable
is expected, it is a bad practice to do so.
The crux of the issue is that Thread
is a larger concept than Runnable
. A Runnable
represents a task. A
Thread
represents a task and its execution management (ie: how it should behave when started, stopped, resumed, …). It is both a task
and a lifecycle management.
Noncompliant code example
public static void main(String[] args) {
Thread runnable = new Thread() {
@Override
public void run() { /* ... */ }
};
new Thread(runnable).start(); // Noncompliant
}
Compliant solution
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() { /* ... */ }
};
new Thread(runnable).start();
}